﻿using System;
using System.Collections.Generic;
using System.Reflection;

namespace Red.Scripts.Project.Generator
{
    public class Program
    {
        enum Error
        {
            Success,
            CouldNotLoadProjectTemplate,
            CouldNotSaveProjectTemplate,
        }

        static int Main( string[] args )
        {
            CommandLine commandLine = new CommandLine( args );

            Log.Silent = commandLine.HasParameter( "silent" );

            Log.WriteLine( $"Red Scripts Project Generator v{Assembly.GetExecutingAssembly().GetName().Version}\n" );

            Log.WriteLine( "Scanning scripts directory for files" );
            FileEnumerator enumerator = new FileEnumerator();
            enumerator.Extensions.Add( ".ws" );
            enumerator.Extensions.Add( ".script" );
            enumerator.Enumerate( commandLine[ "sourcedir" ] );

            Log.WriteLine( "Creating project file" );
            Project.Template template = new Project.Template()
            {
                SourceFile = commandLine[ "project-template" ],
            };

            Log.WriteLine( "Reading existing project properties" );
            Project.Reader reader = new Project.Reader()
            {
                SourceFile = commandLine[ "project-target" ]
            };

            if ( reader.Load() )
            {
                Log.WriteLine( "Restoring previous project settings" );

                template.Properties = reader.Properties;
            }

            SetPropertyDefaultValue( "ProjectGuid", Guid.NewGuid(), template.Properties );
            SetPropertyDefaultValue( "ScriptsRoot", commandLine[ "sourcedir" ], template.Properties );
            SetPropertyDefaultValue( "ScriptsBinary", commandLine[ "binary" ], template.Properties );

            template.Files.AddRange( enumerator.Files );

            if ( !template.Load() )
                return (int)Error.CouldNotLoadProjectTemplate;

            if ( !template.Write( commandLine[ "project-target" ] ) )
                return (int)Error.CouldNotSaveProjectTemplate;

            Log.WriteLine( "Creating Solution file" );
            Solution.Template solution = new Solution.Template()
            {
                SourceFile = commandLine[ "solution-template" ],
                ProjectInstanceGuid = (Guid)template.Properties[ "ProjectGuid" ].Object
            };
            solution.Read();
            solution.Write( commandLine[ "solution-target" ] );

            return (int)Error.Success;
        }

        static void SetPropertyDefaultValue<T>( string name, T value, IDictionary<string, Project.IProperty> properties )
        {
            if ( !properties.ContainsKey( name ) )
            {
                Project.Property<T> property = new Project.Property<T>()
                {
                    IsSet = true,
                    Name = name,
                    Value = value
                };

                properties[ name ] = property;
            }
        }
    }
}
